// 为每个 section 添加点击展开/收缩功能
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
    const title = section.querySelector('h2');
    title.addEventListener('click', () => {
        section.classList.toggle('expanded');
        const content = section.querySelectorAll('.timeline, .project-details, ul, p');
        content.forEach(item => {
            item.style.display = section.classList.contains('expanded') ? 'block' : 'none';
        });
    });
});

// 页面加载完成后，将第一个 section 展开
window.addEventListener('load', () => {
    const firstSection = sections[0];
    firstSection.classList.add('expanded');
    const content = firstSection.querySelectorAll('.timeline, .project-details, ul, p');
    content.forEach(item => {
        item.style.display = 'block';
    });
});

// 导航栏滚动平滑效果
const navLinks = document.querySelectorAll('.navbar a');
navLinks.forEach(link => {
    link.addEventListener('click', (e) => {
        e.preventDefault();
        const targetId = link.getAttribute('href').substring(1);
        const targetElement = document.getElementById(targetId);
        if (targetElement) {
            window.scrollTo({
                top: targetElement.offsetTop - 50,
                behavior: 'smooth'
            });
        }
    });
});

// 新增：滚动时高亮当前显示的导航栏项目
const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        const id = entry.target.getAttribute('id');
        const navLink = document.querySelector(`.navbar a[href="#${id}"]`);
        if (entry.isIntersecting) {
            navLinks.forEach(link => link.classList.remove('active'));
            navLink.classList.add('active');
        }
    });
}, {
    rootMargin: '-30% 0px -70% 0px'
});

sections.forEach(section => {
    observer.observe(section);
});    